package itbour.onetouchshow.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.OneToucDialog;
import com.lsjr.callback.EncryBeanCallBack;
import com.lsjr.utils.HttpUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.config.PictureSelectionConfig;

import java.util.HashMap;

import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.video.videoplay.VideoplayActivity;
import itbour.onetouchshow.base.BaseActivity;
import itbour.onetouchshow.constants.AppConfig;
import itbour.onetouchshow.constants.platform.WorkPlatform;
import itbour.onetouchshow.utils.video.EditPlayVideoFrames;

/**
 * Created by onetouch on 2017/11/25.
 */

public class JumpUtils {


    public static final String VIDEOACTIVITY_TAG = "moudleresult";

    /**
     * 跳转到视频播放
     *
     * @param activity
     */
    public static void goToVideoPlayer(Context activity, int moduleId) {
        loadThisPageInfo(activity, moduleId);

    }


    /**
     * 获取模板预览
     */
    public static void loadThisPageInfo(Context activity, int id) {
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("device", 610);
        stringStringHashMap.put("id", id);
        HttpUtils.getInstance().executeGet(AppConfig.GET_VERTICAL_MODEL_PREVIEW_V10, stringStringHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                T_.showToastReal(exception);
            }

            @Override
            protected void onSuccess(String response) {
                L_.i("response===" + response);
                Intent intent = new Intent(activity, VideoplayActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(WorkPlatform.TYPE, WorkPlatform.getWorkPlatform(WorkPlatform.MODULE));
                bundle.putString(VIDEOACTIVITY_TAG, response);
                //bundle.putSerializable("item",  videoItem);
                intent.putExtras(bundle);
                activity.startActivity(intent);
            }
        });
    }


    public static void gotoSelectType(BaseActivity context) {
        int chooseMode = PictureMimeType.ofVideo();
        // 进入相册 以下是例子：不需要的api可以不写
        PictureSelector.create(context)
                .openGallery(chooseMode)// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .theme(R.style.picture_default_style)// 主题样式设置 具体参考 values/styles   用法：R.style.picture.white.style
                .maxSelectNum(1)// 最大图片选择数量
                .setOnSelcterVideoFilter(new PictureSelectionConfig.OnSelectClickListner() {

                    @Override
                    public boolean ishasFilter(String url, Activity activity) {
                        if (url != null) {
                            if (EditPlayVideoFrames.getVideoEncodeWithPath(url)) {
                                double durationCount = TypeCastHelper.toDouble(AssetsDataUtils.videoWHDA(url)[2]) / 1000 * 1.0;
                                if (durationCount < 3) {
                                    showUpdateWorkDialog(activity, false, true, false);
                                    return false;
                                }
                                if (durationCount > 180) {
                                    showUpdateWorkDialog(activity, false, false, true);
                                    return false;
                                }
                                return true;
                            } else {
                                showUpdateWorkDialog(activity, true, false, false);
                                return false;
                            }
                            // L_.e(videoEncodeWithPath);
                        }
                        return false;
                    }
                })
                .minSelectNum(1)// 最小选择数量
                .imageSpanCount(4)// 每行显示个数
                .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                .isCamera(false)// 是否显示拍照按钮
                .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                .enableCrop(false)// 是否裁剪
                .compress(true)// 是否压缩
                .synOrAsy(true)//同步true或异步false 压缩 默认同步
                .glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                .isGif(false)// 是否显示gif图片
                .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                .circleDimmedLayer(false)// 是否圆形裁剪
                .showCropFrame(true)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false
                .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false
                .openClickSound(false)// 是否开启点击声音
                //.selectionMedia(false)// 是否传入已选图片
                .minimumCompressSize(100)// 小于100kb的图片不压缩
                .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code

    }

    public static void showUpdateWorkDialog(Context context, boolean type, boolean isShort, boolean isLong) {
        String contexttext = null;
        if (type) {
            // contexttext = "无法获取视频信息, 请重新选择";
            contexttext = "仅支持mov、mp4格式视频，请重新选择视频上传";
        }
        if (isShort) {
            contexttext = "视频过短, 请选择超过3秒的视频";
        }
        if (isLong) {
            contexttext = "视频过长, 请选择小于3分钟的视频";
        }
        String finalContexttext = contexttext;
        new OneToucDialog.Builder(context)
                .content(finalContexttext)
                .positiveText("确定").onPositive(new OneToucDialog.SingleButtonCallback() {
            @Override
            public void onClick(OneToucDialog dialog, DialogAction which) {
            }
        }).setType(OneToucDialog.Builder.ONE_TOUCH_TYPE)
                .cancelable(false)
                .show();
    }


}
